﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using ArbolandiaBL;
using Ext.Net;

public partial class catalogContentManager : System.Web.UI.Page
{
    protected void Page_Init(object sender, EventArgs e)
    {
        this.WindowEditorUserControlDetail.GridStore = this.Store1;
    }
    protected void Page_PreLoad(object sender, EventArgs e)
    {

        if (!ArbolandiaBL.Security.ValidLogin())
        {
            X.AddScript("this.parent.LogOut();");
        }

    }
    protected void Page_Load(object sender, EventArgs e)
    {
        WindowEditorUserControlDetail.reloadgrid += new ASP.admin_catalogcontentmanagerdetailctrl_ascx.ReloadGridFromUsr(ReloadGridFromUc);
        if (!X.IsAjaxRequest)
        {
            ReloadGrid();
        }
    }
    public void ReloadGridFromUc()
    {
        ReloadGrid();
    }
    [DirectMethod]
    public void ShowDetails(int id)
    {
        var content = new ContentManagement();
        if (id > 0)
        {
            content = new Service().GetContent(id);
        }
        this.WindowEditorUserControlDetail.SetDetail(content);
        this.WindowEditorUserControlDetail.Show();
    }

    protected void btnSearch_Click(object sender, DirectEventArgs e)
    {
        ReloadGrid();
    }
    protected void Store1_RefreshData(object sender, StoreReadDataEventArgs e)
    {
        ReloadGrid();
    }
    protected void ReloadGrid()
    {
        try
        {

            this.Store1.DataSource = new Service().GetContents(0);
            this.Store1.DataBind();

        }
        catch (Exception ex)
        {
            //unexpected error
            X.Msg.Show(new MessageBoxConfig { Title = "Error", Message = ex.Message, Buttons = MessageBox.Button.OK, Icon = MessageBox.Icon.ERROR });
        }
    }
    [DirectMethod]
    public void Delete(int id)
    {
        try
        {
            //Call Service Delete
            new Service().DeleteContent(id);
            ReloadGrid();

            Ext.Net.Notification.Show(new Ext.Net.NotificationConfig
            {
                Title = Constants.TITLE_DELETE,
                Icon = Ext.Net.Icon.PageSave,
                AutoHide = true,
                Html = Constants.NOTIFY_DELETED
            });
        }
        catch (Exception ex)
        {
            //unexpected error
            X.Msg.Show(new MessageBoxConfig { Title = "Error", Message = ex.Message, Buttons = MessageBox.Button.OK, Icon = MessageBox.Icon.ERROR });
        }
    }
}